﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EM.Components.Entities;
using EM.Web.Models;
using System.Data.Objects;

namespace EM.Web.Controllers
{
    public class LeadsController : Controller
    {
        private LeadsService _model;
        public LeadsController() 
        {
            _model = new LeadsService();
        }
        
        //
        // GET: /Leads/

        public ActionResult Index()
        {
            return View(_model.GetAll());
        }

        //
        // GET: /Leads/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Leads/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Leads/Create

        [HttpPost]
        public ActionResult Create(EM_Leads lead)
        {
            if (!ModelState.IsValid) return View("Edit",lead);
            try
            {
                _model.Add(lead);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Leads/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View(_model.Get(id));
        }

        //
        // POST: /Leads/Edit/5

        [HttpPost]
        public ActionResult Edit(EM_Leads entityToChange)
        {
            try
            {
                _model.Update(entityToChange);
                return RedirectToAction("Index");
            }
            catch
            {
               return View();
            }
        }

        //
        // GET: /Leads/Delete/5

        //public ActionResult Delete(int id)
        //{
        //    _model.Delete(id);
        //    return RedirectToAction("Index");
        //}

        //
        // POST: /Leads/Delete/5

        [HttpPost]
        public ActionResult Delete(int id)
        {
            try
            {
                _model.Delete(id);
                return Content(Boolean.TrueString);
            }
            catch
            {
                return Content(Boolean.FalseString);
            }
        }
    }
}
